<?php
class DBCLASS{
	function error_report(){
		/*echo '<script language="javascript"> location.href="?mod=home";</script>';*/
		exit();
	}
	/* function exec sql */
	function SQLexec($sql){
	 	$rs = mysql_query($sql) or $this->error_report();
		return $rs;
	}
	function SQLrecordset($sql){
		if($sql){
			$rs = mysql_query($sql) or $this->error_report();
			$row =  mysql_fetch_array($rs);	
			return $row;
		}
	}
	/* function GetPriKey: Get Name of Primary key of a table */
	function GetPriKey($table){
		global $conn,$DP;
		$sql_get_key = "SHOW COLUMNS FROM ".$DP.$table; 
		$rs_key = $this->SQLexec($sql_get_key);//mysql_query($sql_get_key); 
		while($row_key = mysql_fetch_assoc($rs_key)){ 
			if($row_key[Key] == 'PRI') $primary_key = $row_key[Field] ; 
		}
		return $primary_key;
	}
	/*function ValidateString($table){
		global $conn,$DP;
		$sql_get_key = "SHOW COLUMNS FROM ".$DP.$table; 
		$rs_key = mysql_query($sql_get_key); 
		while($row_key = mysql_fetch_assoc($rs_key)){ 
			echo $row_key[Type].'<br>';
		}
	}*/
	/* function GetFieldValue: Get Field value of a table */
	function GetFieldValue($table, $id, $field_name){
		global $conn,$DP;
		$pri_key = $this->GetPriKey($table);
		$sql = "SELECT {$field_name} FROM {$DP}{$table} WHERE {$pri_key}='$id' ";
		$row = $this->SQLrecordset($sql);//recordset($sql);
		$value = $row[$field_name];
		return $value;	
	}	
	/* function */
	function DBGetField($table, $where, $field_name){
		global $conn,$DP;
		$sql = "SELECT {$field_name} FROM {$DP}{$table} WHERE {$where}";
		$row = $this->SQLrecordset($sql);//recordset($sql);
		$value = $row[$field_name];
		return $value;	
	}
	/* function UpdateOneField: Update a value to table, return true/false */
	function UpdateOneField($table, $field, $value, $primary_key_value){
		global $conn,$DP;
		$pri_key = $this->GetPriKey($table);
		$sql = "UPDATE {$DP}{$table} SET {$field}='{$value}' WHERE {$pri_key}='{$primary_key_value}'";
		if (count($this->SQLexec($sql)) > 0) //execSQL($sql)
			return true;
		else 
			return false;
	}

	function ZenDataList($table,$fields,$where,$orderby,$limit){
		global $dbconn,$DP;
		if($fields == ""){
			$fields = "*";
		}
		if($orderby != ''){
			$orderby = "ORDER BY ".$orderby;
		}
		if($limit != ''){
			$limit = "LIMIT ".$limit;
		}
		if($where != ''){
			$where = "WHERE ".$where;
		}

		$sql = "SELECT {$fields} FROM {$DP}{$table} {$where} {$orderby} {$limit}";
		//return mysql_query($sql);

		return $this->SQLexec($sql);
	}
	function FetchData($table,$fields,$where){
		global $dbconn,$DP;
		$sql = "SELECT {$fields} FROM {$DP}{$table} {$where}";
		//$rs = mysql_query($sql);
		//return mysql_fetch_array($rs);
	 	return $this->SQLrecordset($sql);
	}
	/*function _data_join($table,$join_to,$fields,$where,$orderby,$limit){
		global $DP,$dbconn;
	}*/
	function DBInsert($table, $POST){
		global $dbconn,$DP;
		$sql_COLUMNS = "SHOW COLUMNS FROM {$DP}".$table;
		$rs_COLUMNS = mysql_query($sql_COLUMNS);
		if(mysql_num_rows($rs_COLUMNS) > 0){
			$sql = "INSERT INTO {$DP}{$table} (";
			$field = ''; $data = '';
			while($row_COLUMNS = mysql_fetch_assoc($rs_COLUMNS)){
				if(array_key_exists($row_COLUMNS[Field],$POST) && $row_COLUMNS[Key]!='PRI'){
					$field.= "{$row_COLUMNS[Field]},";
					$data.= "'". mysql_escape_string($POST[$row_COLUMNS[Field]])."',";
				}
			}
			$field = substr($field,0,strlen($field)-1);	
			$data = substr($data,0,strlen($data)-1);	
			$sql.= $field.") VALUES (".$data.")";
			mysql_query($sql);
			return mysql_insert_id();
		}
	}
	function DBInsert_key($table, $POST){
		global $dbconn,$DP;
		$sql_COLUMNS = "SHOW COLUMNS FROM {$DP}".$table;
		$rs_COLUMNS = mysql_query($sql_COLUMNS);
		if(mysql_num_rows($rs_COLUMNS) > 0){
			$sql = "INSERT INTO {$DP}{$table} (";
			$field = ''; $data = '';
			while($row_COLUMNS = mysql_fetch_assoc($rs_COLUMNS)){
				if(array_key_exists($row_COLUMNS[Field],$POST)){
					$field.= "{$row_COLUMNS[Field]},";
					$data.= "'".mysql_escape_string($POST[$row_COLUMNS[Field]])."',";
				}
			}
			$field = substr($field,0,strlen($field)-1);	
			$data = substr($data,0,strlen($data)-1);	
			$sql.= $field.") VALUES (".$data.")";
			mysql_query($sql);
			return mysql_insert_id();
		}
	}
	/* function DBUpdate: update a record to a table */
	function DBUpdate($table, $POST, $where){
		global $dbconn,$DP;
		$sql_COLUMNS = "SHOW COLUMNS FROM {$DP}".$table;
		$rs_COLUMNS = mysql_query($sql_COLUMNS);
		if(mysql_num_rows($rs_COLUMNS) > 0){
		 	$sql = "UPDATE {$DP}{$table} SET ";
			while($row_COLUMNS = mysql_fetch_assoc($rs_COLUMNS)){
				if(array_key_exists($row_COLUMNS[Field],$POST)){
					$sql.= "`{$row_COLUMNS[Field]}`='".mysql_escape_string($POST[$row_COLUMNS[Field]])."',";
				}
			}
			$sql= substr($sql,0,strlen($sql)-1);	
			$sql.= " ".$where;
			mysql_query($sql);
		}
	}
	function DBDelete($table,$where){
		global $dbconn,$DP;
		$sql = "DELETE FROM {$DP}{$table} {$where}";
		mysql_query($sql);
	}
	function DBGetNumRows($table,$where){
		global $dbconn,$DP;
		$pri_key = $this->GetPriKey($table);
		$sql = "SELECT {$pri_key} FROM {$DP}{$table} {$where}";
		$rs=mysql_query($sql);
		return mysql_num_rows($rs);
	}
	//////////////////////
}
?>